package com.li.my_tiny_spring.beans.reader;

import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.li.my_tiny_spring.beans.definition.BeanDefinition;
import com.li.my_tiny_spring.util.io.Resource;
import com.li.my_tiny_spring.util.io.ResourceLoader;

public abstract class AbstractBeanDefinitionReader implements BeanDefinitionReader {

	private Map<String, BeanDefinition> registry = new ConcurrentHashMap<>();

	private ResourceLoader resourceLoader;
	protected AbstractBeanDefinitionReader(ResourceLoader resourceLoader) {
		this.resourceLoader =  resourceLoader;
	}
	@Override
	public final  Map<String, BeanDefinition> getRegistry() {
		return registry;
	}

	@Override
	public void loadBeanDefinitions(String location) {
		Resource resource = resourceLoader.getResource(location);
		InputStream inputStream = resource.getInputStream();
		this.registry = doLoadBeanDefinitions(inputStream);
	}

	protected abstract Map<String,BeanDefinition> doLoadBeanDefinitions(InputStream inputStream);
}
